//////////////////////////////////////////////////////
// MyEngine.cpp
//////////////////////////////////////////////////////

#include "myengine.h"

//////////////////////////////////////////////////////
// CMyEngine()
//////////////////////////////////////////////////////
CMyEngine::CMyEngine(char* pstrMap, int iMapType, int iDrawSector, int iSector) :
    CEngine(pstrMap, iMapType, iDrawSector, iSector)
{
    m_bBuyingPotion = FALSE;
    m_bBuyingArmor = FALSE;
    m_bBuyingWeapon = FALSE;
    m_bRentingRoom = FALSE;
}

//////////////////////////////////////////////////////
// ~CMyEngine()
//////////////////////////////////////////////////////
CMyEngine::~CMyEngine(void)
{
}

//////////////////////////////////////////////////////
// RespondToPlayerLocation()
//////////////////////////////////////////////////////
void CMyEngine::RespondToPlayerLocation()
{
    switch (m_iCurrentMap)
    {
        case TRAININGGROUNDS:
			HandleTrainingGrounds();
			break;

		case BATTLEMAP:
			HandleBattleMap();
        break;
    }
}

//////////////////////////////////////////////////////
// HandleNPC()
//////////////////////////////////////////////////////
void CMyEngine::HandleNPC(int iNPC)
{
   /* char name[128];
    strcpy(name, m_People.GetPerson(iNPC)->GetName());
    strcpy(name, &name[1]);
    name[strlen(name) - 1] = 0;
    if (strcmp(name, "Fennel Gridrock") == 0)
        HandleFennelGridrock();
    else if (strcmp(name, "Uris Yamdolf") == 0)
        HandleUrisYamdolf();
   
    else
        CEngine::HandleNPC(iNPC);
		*/
}

//////////////////////////////////////////////////////
// HandleContainer()
//////////////////////////////////////////////////////
BOOL CMyEngine::HandleContainer(int iSector)
{
	/*
    if (m_iCurrentMap == TrainingGroundsDUNGEON02 && 
            m_Sectors[iSector] == BARREL_DUN)
    {
        FinishWillamQuest();
        return TRUE;
    }
    else if (m_iCurrentMap == TrainingGroundsDUNGEON01LEVEL02 && 
            m_Sectors[iSector] == BARREL_DUN)
    {
        FinishUniformQuest();
        return TRUE;
    }
    else
        return CEngine::HandleContainer(iSector);
		*/
	return false;
}

//////////////////////////////////////////////////////
// HandleDoor()
//////////////////////////////////////////////////////
BOOL CMyEngine::HandleDoor(int iSector)
{
/*    if (m_iCurrentMap == TrainingGrounds && iSector == HOUSE02FRONTDOOR)
    {
        BOOL bOpen = OpenAbandonedHouse();
        return bOpen;
    }
    return CEngine::HandleDoor(iSector);
	*/

	return false;
}

//////////////////////////////////////////////////////
// IsBattleLocation()
//////////////////////////////////////////////////////
BOOL CMyEngine::IsBattleLocation()
{
    //switch (m_iCurrentMap)
   // {
   // case BattleMap:
   //     return TRUE;
   // }
   return FALSE;
}

//////////////////////////////////////////////////////
// GetBattleMap()
//////////////////////////////////////////////////////
CMonster* CMyEngine::GetBattleMap()
{

	int sector = 0;

  //  CMonster* m_pCurrentMonster = new CMonster();
    switch(GetCurrentMap())
    {
    case TRAININGGROUNDS:
		HandleOpenBattle();
        break;
    }
    return m_pCurrentMonster;
}

//////////////////////////////////////////////////////
// SaveGame()
//////////////////////////////////////////////////////
void CMyEngine::SaveGame(BOOL bFullSave)
{
    //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
    char dir[10];
    if (bFullSave)
    {
        strcpy(dir, "Save");
        DeleteSaveFiles();
        CopyTempToSave();
    }
    else
        strcpy(dir, "Temp");
    //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
    SaveMapFile(bFullSave);
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    //char dir[10];
    //if (bFullSave)
    //    strcpy(dir, "Save");
    //else
    //    strcpy(dir, "Temp");
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    char fileName[MAX_PATH];
    wsprintf(fileName, "%s\\SaveFile.dat", dir);
    std::ofstream SaveFile(fileName);

    // Save quest flags.
  //  SaveFile << bSpokeToGrewell << '\n';


    // save player stats.
    SaveFile << m_Player.GetArmor() << '\n';
    SaveFile << m_Player.GetExperience() << '\n';
    SaveFile << m_Player.GetGold() << '\n';
    SaveFile << m_Player.GetHitPoints() << '\n';
    SaveFile << m_Player.GetKeys() << '\n';
    SaveFile << m_Player.GetMaxHitPoints() << '\n';
    SaveFile << m_Player.GetPotions() << '\n';
    SaveFile << m_Player.GetSector() << '\n';
    SaveFile << m_Player.GetWeapon() << '\n';

    // Save Map info.
    SaveFile << m_pstrCurrentMapFile << '\n';
    SaveFile << GetCurrentMap() << '\n';

    SaveFile.close();
    if (bFullSave)
        TurnOnPlaque("Game Saved");
}

//////////////////////////////////////////////////////
// LoadGame()
//////////////////////////////////////////////////////
void CMyEngine::LoadGame()
{
    //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
    DeleteTempFiles();
    CopySaveToTemp();
    //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN

    char buf[128];
    std::ifstream SaveFile("Save\\SaveFile.dat", std::ios::binary);

    // Load quest flags.
 

    // Load player stats.
    GetStringFromFile(SaveFile, buf);
    m_Player.SetArmor(atoi(buf));
    GetStringFromFile(SaveFile, buf);
    m_Player.SetExperience(atoi(buf));
    GetStringFromFile(SaveFile, buf);
    m_Player.SetGold(atoi(buf));
    GetStringFromFile(SaveFile, buf);
    m_Player.SetHitPoints(atoi(buf));
    GetStringFromFile(SaveFile, buf);
    m_Player.SetKeys(atoi(buf));
    GetStringFromFile(SaveFile, buf);
    m_Player.SetMaxHitPoints(atoi(buf));
    GetStringFromFile(SaveFile, buf);
    m_Player.SetPotions(atoi(buf));
    GetStringFromFile(SaveFile, buf);
    m_Player.SetSector(atoi(buf));
    GetStringFromFile(SaveFile, buf);
    m_Player.SetWeapon(atoi(buf));

    // Load map info.
    GetStringFromFile(SaveFile, buf);
    strcpy(m_pstrCurrentMapFile, buf);
    GetStringFromFile(SaveFile, buf);
    SetCurrentMap(atoi(buf));

    SaveFile.close();
    OpenMapFiles(m_pstrCurrentMapFile, FALSE);
    switch (m_byMapType)
    {
    case TRAININGGROUNDS:
        GetCDirect3DObj()->SetCurrentTileSurface
            (GetCDirect3DObj()->GetTrainingGroundsSurfaces());
        break;
 
    }

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    //DeleteTempFiles();
    //CopySaveToTemp();
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    ResetAnimation();
    TurnOnPlaque("Game Loaded");
}

//////////////////////////////////////////////////////
// HandleKeys()
//////////////////////////////////////////////////////
void CMyEngine::HandleKeys(WPARAM wParam)
{
    switch (wParam)
    {
    case VK_ESCAPE:
    case VK_SPACE:
    case 73: //I
        if (!m_bBuyingPotion && !m_bBuyingArmor && !m_bBuyingWeapon)
            CEngine::HandleKeys(wParam);
        break;
    default:
        CEngine::HandleKeys(wParam);
    }
}

//////////////////////////////////////////////////////
// HandleN()
//////////////////////////////////////////////////////
void CMyEngine::HandleN()
{
    if (m_bBuyingPotion)
    {
        m_bBuyingPotion = FALSE;
        TurnOffPlaque();
    }
    else if (m_bBuyingArmor)
    {
        m_bBuyingArmor = FALSE;
        TurnOffPlaque();
    }
    else if (m_bBuyingWeapon)
    {
        m_bBuyingWeapon = FALSE;
        TurnOffPlaque();
    }
    else if (m_bRentingRoom)
    {
        m_bRentingRoom = FALSE;
        TurnOffPlaque();
    }
    else
        CEngine::HandleN();
}

//////////////////////////////////////////////////////
// HandleY()
//////////////////////////////////////////////////////
void CMyEngine::HandleY()
{
    if (m_bBuyingPotion)
        HandleBuyingPotion();
    else if (m_bBuyingArmor)
        HandleBuyingArmor();
    else if (m_bBuyingWeapon)
        HandleBuyingWeapon();
    else if (m_bRentingRoom)
        HandleRentingRoom();
    else
        CEngine::HandleY();
}

//////////////////////////////////////////////////////
// HandleBuyingPotion()
//////////////////////////////////////////////////////
void CMyEngine::HandleBuyingPotion()
{
    if (m_Player.GetGold() > m_iPurchasePrice)
    {
        m_Player.SetGold(m_Player.GetGold() - m_iPurchasePrice);
        m_Player.SetPotions(m_Player.GetPotions() + 1);
        char msg[80];
        wsprintf(msg, "You buy 1 potion for %d gold.", m_iPurchasePrice);
        TurnOnPlaque(msg);
    }
    else
        TurnOnPlaque("You don't have enough gold.");
    m_bBuyingPotion = FALSE;
}

//////////////////////////////////////////////////////
// HandleBuyingArmor()
//////////////////////////////////////////////////////
void CMyEngine::HandleBuyingArmor()
{
    if (m_Player.GetGold() > m_iPurchasePrice)
    {
        m_Player.SetGold(m_Player.GetGold() - m_iPurchasePrice);
        m_Player.SetArmor(m_iNewArmorLevel);
        char msg[80];
        wsprintf(msg, "You buy level %d armor for %d gold.", 
            m_iNewArmorLevel, m_iPurchasePrice);
        TurnOnPlaque(msg);
    }
    else
        TurnOnPlaque("You don't have enough gold.");
    m_bBuyingArmor = FALSE;
}

//////////////////////////////////////////////////////
// HandleBuyingWeapon()
//////////////////////////////////////////////////////
void CMyEngine::HandleBuyingWeapon()
{
    if (m_Player.GetGold() > m_iPurchasePrice)
    {
        m_Player.SetGold(m_Player.GetGold() - m_iPurchasePrice);
        m_Player.SetWeapon(m_iNewWeaponLevel);
        char msg[80];
        wsprintf(msg, "You buy a level %d weapon for %d gold.", 
            m_iNewWeaponLevel, m_iPurchasePrice);
        TurnOnPlaque(msg);
    }
    else
        TurnOnPlaque("You don't have enough gold.");
    m_bBuyingWeapon = FALSE;
}

//////////////////////////////////////////////////////
// HandleRentingRoom()
//////////////////////////////////////////////////////
void CMyEngine::HandleRentingRoom()
{
    if (m_Player.GetGold() > m_iPurchasePrice)
    {
        m_Player.SetGold(m_Player.GetGold() - m_iPurchasePrice);
        m_Player.SetHitPoints(m_Player.GetMaxHitPoints());
        TurnOnPlaque("You have a peaceful night's rest.");
    }
    else
        TurnOnPlaque("You don't have enough gold.");
    m_bRentingRoom = FALSE;
}

//////////////////////////////////////////////////////
// SetPurchasePrice()
//////////////////////////////////////////////////////
void CMyEngine::SetPurchasePrice(int iPrice)
{
    m_iPurchasePrice = iPrice;
}

//////////////////////////////////////////////////////
// SetNewWeaponLevel()
//////////////////////////////////////////////////////
void CMyEngine::SetNewWeaponLevel(int iLevel)
{
    m_iNewWeaponLevel = iLevel;
}

//////////////////////////////////////////////////////
// SetNewArmorLevel()
//////////////////////////////////////////////////////
void CMyEngine::SetNewArmorLevel(int iLevel)
{
    m_iNewArmorLevel = iLevel;
}
